package edu.uc.ui;

import java.io.Serializable;
import java.util.List;

import com.foodview1.R;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import edu.uc.dto.Review;
import edu.uc.persistence.ReviewDAO;
import edu.uc.persistence.ReviewDAOInterface;

/**
 * @author Ckorczyk1
 *
 *This class contains the code for the users past reviews
 */
public class myReviewsActivity extends ListActivity {
	
	public static final String REVIEW_RESULT = "reviewResult";
	
	// declare our DAO.
	ReviewDAOInterface reviewList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		reviewList = new ReviewDAO(this);
		
		// Create SessionUser global preference list
		final SharedPreferences SessionUserMgr = PreferenceManager.getDefaultSharedPreferences(this);

		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		List<Review> userReviews;
		try {
			
			// Define the User Id
			String USER_ID = (SessionUserMgr.getString("User_Id", ""));

			// search for reviews that meet criteria.
			userReviews = reviewList.getUserReviews(USER_ID);

			// pass the list of plants that meet the criteria to the ArrayAdapter.
			ArrayAdapter<Review> reviewAdapter = new ArrayAdapter<Review>(this, android.R.layout.simple_list_item_1, userReviews);

			// tell this screen what we want to show.
			setListAdapter(reviewAdapter);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "No reviews found!", Toast.LENGTH_LONG).show();
		}

	}

	/**
	 * This method is invoked when a list item is clicked.
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		// find out what we clicked.
		Review selectedPlant = (Review) getListAdapter().getItem(position);

		// call back to the screen that called us.
		// get a reference to the intent that invoked us.
		Intent intent = this.getIntent();

		// create a bundle for return data.
		Bundle data = new Bundle();
		// place our result data in this bundle.
		data.putSerializable(REVIEW_RESULT, (Serializable) selectedPlant);

		// associate the bundle with the intent.
		intent.putExtras(data);
		// set the result, and finish, to go back to the calling screen.
		this.setResult(RESULT_OK, intent);
		finish();
	}
	
	//variables for menu items
		public static final int SEARCH_RESTAURANTS = 1;
		public static final int REVIEW_MEAL = 2;
		public static final int NEARBY_RESTAURANTS = 3;
		public static final int SHOW_MAP = 4;
		public static final int MY_REVIEWS = 5;

		/**
		 * onCreateMethod
		 */
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			getMenuInflater().inflate(R.menu.activity_main, menu);
			
//			menu.add(Menu.NONE, SEARCH_RESTAURANTS, Menu.FIRST, "Search");
//			menu.add(Menu.NONE, REVIEW_MEAL, Menu.FIRST+1, "Review Meal");
//			menu.add(Menu.NONE, NEARBY_RESTAURANTS, Menu.FIRST+2, "Nearby Restaurants");
//			menu.add(Menu.NONE, SHOW_MAP, Menu.FIRST+3, "Show Map");
			return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			super.onOptionsItemSelected(item);
			
			switch (item.getItemId()) {
			case R.id.SEARCH_RESTAURANTS:
				searchRestaurants();
				break;
			case R.id.SHOW_MAP:
				showMap();
				break;
			case R.id.MY_REVIEWS:
				myReviews();
				break;
			case R.id.MAIN:
				home();
				break;
			case R.id.REVIEW_FOOD:
				reviewFood();
				break;
			default:
				// this is what happens if there is not a case that matches item.getItemId().
			}
			
			return true;
			
			
		}
		// method showMap - brings up the map screen to display nearby restaurants
		private void showMap() {
			Intent showMapRestaurants = new Intent(this, mapRestaurantActivity.class);
			startActivity(showMapRestaurants);
			
		}
		// method searchRestaurants - Brings up the search screen containing search criteria
		private void searchRestaurants() {
			Intent searchRestaurants = new Intent(this, SearchActivity.class);
			startActivity(searchRestaurants);
			
		}
		//method myReviews - brings up the screen with a list of all of the users previous reviews
		private void myReviews() {
			Intent myReviews = new Intent(this, myReviewsActivity.class);
			startActivity(myReviews);
		}

		// method home - returns the user to the home screen
		private void home() {
			Intent home = new Intent(this, MainActivity.class);
			startActivity(home);
		}
		// method reviewFood - new review screen
		private void reviewFood() {
			Intent reviewFood = new Intent(this, ReviewFoodActivity.class);
			startActivity(reviewFood);
		}
	

}